// pages/tuijian/index.js
const app = getApp();
let globalData = getApp().globalData;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    windowHeight: '',
    user_id: '',//经纪人id
    quan: false,//业主显示优惠券
    quan_read: false,
    identity: '',//身份
    qian_img: globalData.qian_img,
    quan_arr: {},
    tjcurrentId: '0',
    tuijian_read: false, // 推荐须知
    jiangli_read: false, // 奖励政策
    lianma_read: false, // 这是什么
    fuli_read: false, //我的注册礼
    inputValue: null,
    quan_ling: false,//去领取
    quan_use: false,//使用
    quan_used: false,//已使用
    quan_qrcode: false,//优惠券核销
    quan_name: '',//优惠券的名字
    qrcode_url: '',//核销码
    agent_list:{}, //经纪人列表
    noSend:'', //红包未领取
    red_code:'',//红包码
    invite_qrcode: '',//邀请二维码
    invite_code: ''//邀请码
  },
  //推荐状态
  tuijianTap: function (e) {
    let id = e.currentTarget.id;
    let user_id = this.data.user_id;
    if(id==0){
      this.getAgent(user_id);
    }
    // console.log(id);
    this.setData({
      tjcurrentId: id
    })
  },
  //我的注册礼/领取福利
  tofuli: function (e) {
    let user_id = this.data.user_id;
    let type = e.currentTarget.dataset.type;
    this.getRedcode(user_id,type);
    this.setData({
      fuli_read: true
    })
  },
  //复制邀请码
  tocopy_code:function(e){
    let code_text = e.currentTarget.dataset.text;
    wx.setClipboardData({
      data: code_text,
      success: function (res) {
      }
    })
  },
  //推荐须知
  toxuzhi: function (e) {
    this.setData({
      tuijian_read: true
    })
  },
  //奖励政策
  tojiangli: function (e) {
    this.setData({
      jiangli_read: true
    })
  },
  //优惠券
  toquan: function (e) {
    this.setData({
      quan_read: true
    })
  },
  // 关闭推荐须知/奖励政策
  closeModel() {
    console.log('关闭')
    this.setData({
      tuijian_read: false,
      jiangli_read: false,
      lianma_read: false,
      quan_read: false,
      fuli_read: false,
    })
    console.log(this.data.quan_read);

  },
  //关闭核销
  closeModel_qrcode() {
    this.setData({
      quan_qrcode: false
    })
  },
  totuijian(){
    wx.navigateBack({
      delta: 1
    })
  },
  //去领取
  tolingqu: function (e) {
    let user_id = this.data.user_id;
    let coupons_id = e.currentTarget.dataset.id;
    this.lingquQuan(user_id, coupons_id);
  },
  //使用
  toshiyong: function (e) {
    let coupons_qrcode = e.currentTarget.dataset.qrcode;
    let coupons_name = e.currentTarget.dataset.name;
    this.setData({
      quan_qrcode: true,
      quan_name: coupons_name,
      qrcode_url: coupons_qrcode
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.user_id);
    console.log(options.invite_qrcode);
    console.log(options.invite_code);
    this.setData({
      windowHeight: wx.getSystemInfoSync().windowHeight
    })
    this.setData({
      user_id: options.user_id,
      identity: options.identity,
      invite_qrcode: options.invite_qrcode,
      invite_code: options.invite_code,
    });
    if (this.data.identity == '业主') {
      this.setData({
        quan: true,
      });
      this.getQuan(options.user_id);
    }
    this.getAgent(options.user_id);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //获取经纪人列表数据
  getAgent(e) {
    let data = {
      cid: e,
    }
    globalData.sendPost('custom/invite-list', data).then(res => {
      console.log(res);
      if (res.status == 1) {
        this.setData({
          agent_list: res.data,
          noSend: res.noSend
        })
      }
    })
  },
  //经纪人-获取红包码
  getRedcode(e,type) {
    let data = {
      cid: e,
      type:type
    }
    globalData.sendPost('custom/red-code', data).then(res => {
      console.log(res);
      if (res.status == 1) {
        this.setData({
          red_code: res.code,
        })
      }
    })
  },
  //获取优惠券数据
  getQuan(e) {
    let data = {
      cid: e,
    }
    globalData.sendPost('custom/coupons-info', data).then(res => {
      console.log(res);
      if (res.status == 1) {
        this.setData({
          quan_arr: res.coupons
        })
      }
    })
  },
  //领取优惠券
  lingquQuan(e, id) {
    let data = {
      cid: e,
      coupons_id: id
    }
    globalData.sendPost('custom/get-coupons', data).then(res => {
      console.log(res);
      if (res.status == 1) {
        wx.showToast({
          title: '领取成功',
          icon: 'success',
          duration: 2000,
        });
        this.getQuan(e);
      }
    })
  }
})